﻿/// <reference path="Turbo_common.js" />
/// <reference path="Turbo_Page.js" />
/// <reference path="Turbo_Ajax.js" />
/// <reference path="../General/Cookies.js" />

var _____a;
TA.Turbo.Common._validatorsReady = false;


TA.Turbo.Common.EnsureValidators = function EnsureValidators()
{
	if (!TA.Turbo.Common._validatorsReady)
	{
		if (typeof (TA.Turbo.Common.Validators) === "undefined")
			TA.Turbo.Common.Validators = new Array();

		for (var i = 0; i < TA.Turbo.Common.Validators.length; ++i)
		{
			TA.Turbo.Common.Validators[i] = $GetObject(TA.Turbo.Common.Validators[i]);
			if (TA.Turbo.Common.Validators[i] == null)
			{
				TA.Turbo.Common.Validators.splice(i, 1);
				--i;
			}
		}

		TA.Turbo.Common._validatorsReady = true;
	}
}
TA.Turbo.Common._GetPropertiesJson = function _GetPropertiesJson(postControl)
{
	if (_____a)
		postControl = new $PostControl();

	var sb = new StringBuilder();
	var jw = new JsonWriter(sb);

	jw.StartObject();

	for (var iProperty = 0; iProperty < postControl.Properties.length; ++iProperty)
	{
		var prName = postControl.Properties[iProperty],
			prShortName = postControl.get_Control().get_Object()._propertyDefinitions[prName]._shortName,
			prValue = postControl.get_Control().get_Object()["get_" + prName]();

		jw.PropertyName(prShortName);
		jw.Value(prValue);
		//		jw.Write(prValue);
	}

	jw.EndObject();

	return sb.toString();
}
TA.Turbo.Common.RgxSplitMultiValue = new RegExp("[^,;]+");
TA.Turbo.Common.RgxGetHashString = new RegExp("#(.*)");
var $DoPostBack = TA.Turbo.Common.DoPostBack = function DoPostBack(eventTarget, eventName, eventArgument, postScenarioId, callBackFunction)
{
	TA.Turbo.Common.EnsureValidators();

	eventTarget = TA.Turbo.Common._DoPostBack_ResolveEventTarget(eventTarget);

	var scenario = null;
	if (postScenarioId != null && postScenarioId != "")
	{
		TA.Turbo.Common._EnsureScenario(postScenarioId);
		scenario = TA.Turbo.Common.PostScenarios[postScenarioId];
		if (scenario == null)
			Throw("Scenario with ID='" + postScenarioId + "' doesn't exist.");
	}
	if (_____a)
		scenario = new $PostScenario();


	if (TA.Turbo.Common._DoPostBack_Validate(scenario))
	{
		scenario.RaiseEvent("Posting");

		TA.Turbo.Common._DoPostBack_Perform(eventTarget, eventName, eventArgument, scenario, callBackFunction);

		scenario.RaiseEvent("Posted");
	}
}
TA.Turbo.Common._DoPostBack_ResolveEventTarget = function _DoPostBack_ResolveEventTarget(eventTarget)
{
	if (eventTarget == null)
		return "__Page";
	if (typeof (eventTarget) == "string")
	{
		var clientId = eventTarget.Replace(TA.Turbo.Common.UniqueIdSeparator, TA.Turbo.Common.ClientIdSeparator);
		eventTarget = document.getElementById(clientId);
	}

	return TA.Turbo.Common.GetControlServerName(eventTarget);
}
TA.Turbo.Common._DoPostBack_Validate = function _DoPostBack_Validate(scenario)
{
	return true;
}
TA.Turbo.Common._DoPostBack_Perform = function _DoPostBack_Perform(eventTarget, eventName, eventArgument, scenario, callBackFunction)
{
	if (_____a)
		scenario = new $PostScenario();

	if (scenario == null)
	{
		// no scenario; post as usual
		theForm["__T"].value = eventTarget;
		theForm["__N"].value = eventName;
		theForm["__A"].value = eventArgument;
		theForm.submit();
	}
	else
	{
		if (scenario.PostMode == PostMode.Ajax)
			TA.Turbo.Common._DoPostBack_Ajax(eventTarget, eventName, eventArgument, scenario, callBackFunction);
		else
			TA.Turbo.Common._DoPostBack_Page(eventTarget, eventName, eventArgument, scenario);
	}
}
